#pragma once
//============================================================================
// Copyright (C) 2013 Brett R. Jones
//
// Issued to public domain 2013
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================

#include "VxDefs.h"
#include "OnlineId.h"

//NOTE: all packets must be a multiple of 16 in length ( to optimize encryption )
//NOTE: all packets must have the following packet header

#ifndef MAX_PKT_LEN
    //NOTE: packets greater than 16384 may be blocked by routers/ satellite etc
    #define MAX_PKT_LEN						16384
#endif //MAX_PKT_LEN

#pragma pack(push) 
#pragma pack(1)

// size 8 bytes
class VxPktHdrPrefix
{
public:
	//=== vars ===//
	U16			m_u16PktLen;	// length of packet ( must be a multiple of 16 )
	U16			m_u16PktType;	// Type of packet ( defined by plugin 1-200..zero is reserved )
	// Please only use 1-200 as values in this field ( other values reserved for rogue connections use )
	U8			m_u8SeqNum;		// sequence number for lost packets and replay attack prevention
	U8			m_u8PktVersion;	// pkt version number
	U16			m_u16PktExtra;	// reserved so id is on 8 byte boundary

	// NOTE: Macro #define ROUND_TO_16BYTE_BOUNDRY( a ) (( a + 15 ) & ~15 ) //round up to even 16 byte boundary
	// is defined in util lib for your convenience in rounding packet length up to a 16 byte boundary

    //=== constructor ===//
    VxPktHdrPrefix();

    //=== return true if valid pkt type and length ===//
    bool IsValidVxPkt( void );

    //! return true if data length is large enough to contain this packet
    bool		IsPktAllHere(int iDataLen);
    //! return length of packet
    U16			GetPktLength( void );
    //! return packet type
    U16			getPktTypeAndPluginNum( void );
	U8			getPktType( void );
	U8			getPluginNum( void );

};

class VxPktHdr : public VxPktHdrPrefix
{
public:
    //=== vars ===//
    OnlineId		m_oSrcOnlineId;
    OnlineId		m_oDestOnlineId;

    //=== methods ===//
    OnlineId&		getSrcOnlineId();
    OnlineId&		getDestOnlineId();

    //! make a copy of this packet
    VxPktHdr * MakeCopy( void );
};

#pragma pack(pop)
